﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace fap
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class 运动控制 : Page
    {
        public 运动控制()
        {
            InitializeComponent();
            try
            {
                首页标签.IsChecked = true;
                连接状态.DataContext = 运动控制数据.西门子PLC;
            }
            catch (Exception ex)
            {

            }
          
        }


        #region       申明函数


        #endregion


        public DispatcherTimer timer;

        void timer_Tick(object sender, EventArgs e)
        {

            Timer1.Text = DateTime.Now.ToString("yyyy-dd-MM HH:mm:ss");
        }



        private void 返回箭头_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //NavigationService.Navigate(窗口.主页);

            this.NavigationService.GoBack();

        }

        private void Page_Initialized(object sender, EventArgs e)
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
            Storyboard baiyun = Resources["箭头移动"] as Storyboard;
            baiyun.Begin();
        }
        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            提示页面 form = new 提示页面("确定要退出程序吗？");
            form.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            form.ShowDialog();
            //   Application.Exit();  //强制所有消息中止，退出所有的窗体，但是若有托管线程（非主线程），也无法干净地退出；  

            // Application.ExitThread(); //强制中止调用线程上的所有消息，同样面临其它线程无法正确退出的问题；  
            if (form.是否)
            {
                form.Close();
                System.Environment.Exit(0);   //这是最彻底的退出方式，不管什么线程都被强制退出，把程序结束的很干净，退出时有可能会抛出异常  
            }
            else
            {
                form.Close();
            }


            //  System.Diagnostics.Process tt = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
            // tt.Kill();//直接杀死与本程序相关的所有进程，有可能会导致数据丢失，但是不会抛出异常。
        }

        private void 鼠标进入(object sender, MouseEventArgs e)
        {
            Image ig = sender as Image;
            switch (ig.Tag.ToString())
            {

                case "退出":
                    {
                        Uri ur = new Uri("/fap;component/素材/公用/退出.png", UriKind.Relative);
                        BitmapImage bp = new BitmapImage(ur);
                        ig.Source = bp;


                        break;
                    }

            }
            TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 300);
            WPF相应类.ScaleEasingAnimationShow(ig, 1, 1.1, 2, timeSpan);
        }
        private void 鼠标离开(object sender, MouseEventArgs e)
        {
            Image ig = sender as Image;
            switch (ig.Tag.ToString())
            {

                case "退出":
                    {
                        Uri ur = new Uri("/fap;component/素材/公用/退出.png", UriKind.Relative);
                        BitmapImage bp = new BitmapImage(ur);
                        ig.Source = bp;


                        break;
                    }


            }
            TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 300);
            WPF相应类.ScaleEasingAnimationShow(ig, 1.1, 1, 2, timeSpan);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button obj = (Button)sender;

                switch (obj.Tag)
                {
                    case "连接PLC":
                        {
                            运动控制数据.连接PLC();

                            break;
                        }
                    case "停止运动":
                        {
                           
                            break;
                        }



                }
                this.Focus();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void 运动控制手动界面切换()
        {
       
        }

      






        #region 键盘输入事件



        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {

           

        }


        #endregion
        private void 左边RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton obj;
            switch (主数据.页面选择)
            {
                case 0:
                    {

                        break;
                    }
                case 1:
                    {
                        obj = 首页标签;
                        TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 200);
                        WPF相应类.ScaleEasingAnimationShow(obj, 1.2, 1, 1, timeSpan);

                        break;

                    }
                case 2:
                    {
                        obj = 感应器屏蔽标签;
                        TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 200);
                        WPF相应类.ScaleEasingAnimationShow(obj, 1.2, 1, 1, timeSpan);

                        break;

                    }


            }




            obj = (RadioButton)sender;
            switch (obj.Name)
            {
                case "首页标签":
                    {

                        try
                        {
                            主数据.页面选择 = 1;

                         

                            运动控制切换框.Content = 窗口.运动控制首页;

                        }
                        catch
                        {

                        }


                        break;
                    }
           
                case "感应器屏蔽标签":
                    {
                        主数据.页面选择 = 2;


                        运动控制切换框.Content = 窗口.运动控制感应器屏蔽页;


                        break;
                    }
             


            }
            if (obj.IsChecked == true)
            {
                TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 500);
                WPF相应类.ScaleEasingAnimationShow(obj, 1, 1.2, 1, timeSpan);
            }



        }

     

        private void Label_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            切换界面(0);

        }

        public void 切换界面(int x)
        {
            switch (x)
            {
                case 0:
                    {
                        NavigationService.Navigate(窗口.主页);

                        break;
                    }

                case 1:
                    {
                        NavigationService.Navigate(窗口.主页);

                        break;
                    }
             
                case 3:
                    {
                        NavigationService.Navigate(窗口.运动控制);

                        break;
                    }
                case 4:
                    {
                        NavigationService.Navigate(窗口.统计);

                        break;
                    }
                case 5:
                    {
                    
                        break;
                    }

            }

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            运动控制切换框.Content = 窗口.运动控制首页;

        }
    }
}
